<?php
namespace Store\Controller;
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
use Store\Controller\StoreController;

class DiscountController extends StoreController{
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys','operate');// 数据集
        $this->assign('action','discount');// 页码
    }
    
    //限时抢购
    public function index(){
        $pMan = M('pXianshi');
        $map['store_id'] = session('store_id');
        $count = $pMan->where($map)->order('xianshi_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->where($map)->order('xianshi_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->display();
    }
    
    
    //增加限时抢购
    public function add(){
        if(IS_POST){
            $title = I('xianshi_name');
            $datas['xianshi_name'] = $title;
            $datas['xianshi_title'] = I('xianshi_title');
            $datas['xianshi_explain'] = I('xianshi_explain');
            $datas['start_time'] = strtotime(I('start_time'));
            $datas['end_time'] = strtotime(I('end_time'));
            $datas['member_id'] = session('user_id');
            $datas['member_name'] = session('user_name');
            $datas['store_id'] = session('store_id');
            $datas['member_name'] = session('store_name');
            $datas['lower_limit'] = I('lower_limit');
            $datas['state'] = 1;
            $re = M('pXianshi')->add($datas);
            if ($re) {
                $this->success("限时抢购添加成功！", U('index'));
            }else{
                $this->error('限时抢购添加失败！');
            }
        }
        $this->display();
    }
    
    
    //修改满减送
    public function edit(){
        if(IS_POST){
            $ids = I('xianshi_id');
            $maps['xianshi_id'] = $ids;
            $title = I('xianshi_name');
            $datas['xianshi_name'] = $title;
            $datas['xianshi_title'] = I('xianshi_title');
            $datas['xianshi_explain'] = I('xianshi_explain');
            $datas['start_time'] = strtotime(I('start_time'));
            $datas['end_time'] = strtotime(I('end_time'));
            $datas['member_id'] = session('user_id');
            $datas['member_name'] = session('user_name');
            $datas['store_id'] = session('store_id');
            $datas['member_name'] = session('store_name');
            $datas['lower_limit'] = I('lower_limit');
            $datas['state'] = 1;
            $re = M('pXianshi')->where($maps)->save($datas);
            if($re !== false){
                $this->success("限时抢购修改成功！",U('index'));
            }
        }
        
        $id = I('id');
        $map['xianshi_id'] = $id;
        $pMan = D('pXianshi');
        $re = $pMan->where($map)->find();
        $this->assign('re',$re);
        $this->display();
    }
    
    
    
    
    public function addGoods(){
        $id = I('id');
        $where['xianshi_id'] = $id;
        
        //获取商家的商品
        $map['store_id'] = session('store_id');
        $map['goods_sale'] = 1;
        $map['goods_state'] = 1;
        $order = M("goods");
        $count = $order->where($map)->count();

        $Page = new \Think\Page($count, 12); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $this->assign('page',$show);
        $list = $order->where($map)->order('goods_created desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $re = M('pXianshi')->where($where)->find();
        $arr = M('pXianshiGoods')->where($where)->select();
        $arrs = json_encode($arr);
        $this->assign('arrs',$arrs);
        $this->assign('re',$re);
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    
    public function getOneGoods(){
        $good_id = I('goods_id');
        $map['goods_id'] = $good_id;
        $re = M('goods')->where($map)->find();
        if($re){
            $arr['price'] =$re['goods_price'];
            $arr['title'] =$re['goods_title'];
            $arr['gid'] =$re['goods_id'];
            $arr['pics'] =$re['goods_thumb']."!100x100";
            $arr['result'] = TRUE;
        }else{
            $arr['message'] = "商品已删除或者已下架！";
            $arr['result'] = FALSE;
        }
        return $this->ajaxReturn($arr);
    }
    
    public function getOneGood(){
        $good_id = I('goods_id');
        $map['xianshi_goods_id'] = $good_id;
        $re = M('pXianshiGoods')->where($map)->find();
        if($re){
            $arr['price'] =$re['goods_price'];
            $arr['fprice'] =$re['xianshi_price'];
            $arr['title'] =$re['goods_name'];
            $arr['gid'] =$re['xianshi_goods_id'];
            $arr['pics'] =$re['goods_image']."!100x100";
            $arr['result'] = TRUE;
        }else{
            $arr['message'] = "商品已删除或者已下架！";
            $arr['result'] = FALSE;
        }
        return $this->ajaxReturn($arr);
    }
    
    
    
    public function getEditGoods(){
        $xianshi_goods_id = I('xianshi_goods_id');
        $goods_price = I('goods_price');
        $map['xianshi_goods_id'] = $xianshi_goods_id;
        $datas['xianshi_price'] = I('xianshi_price');
        
        $datas['xianshi_discount'] = Discount($goods_price,I('xianshi_price'));
        $re = M('pXianshiGoods')->where($map)->save($datas);
        if($re){
            //$arr['xianshi_goods'] = M('pXianshiGoods')->where($map)->find();
            $arr['xianshi_price'] = $datas['xianshi_price'];
            $arr['xianshi_discount'] = $datas['xianshi_discount'];
            $arr['message'] = "商品编辑成功！";
            $arr['result'] = TRUE;
        }else{
            $arr['message'] = "商品编辑失败！";
            $arr['result'] = FALSE;
        }
        return $this->ajaxReturn($arr);
    }
    
    
    public function getGoods(){
        $good_id = I('goods_id');
        
        $datas['xianshi_id'] = I('xianshi_id');
        $datas['goods_id'] = $good_id;
        $datas['xianshi_price'] = I('xianshi_price');
        
        $xianshi = M('pXianshi')->where('xianshi_id='.$datas['xianshi_id'])->find();
        
        //商品不能重复(在活动开始后一个商品只能一次)
        $map['goods_id'] = I('goods_id');
        $map['end_time'] = array('elt',$xianshi['end_time']);
        $res = M('pXianshiGoods')->where($map)->find();
        if($res){
            $arr['result'] = FALSE;
            $arr['message'] = "商品已经是活动产品，不能重复添加！";
        }else{
            $map['goods_id'] = $good_id;
            $rea = M('goods')->where($map)->find();
            
            $datas['xianshi_name'] = $xianshi['xianshi_name'];
            $datas['xianshi_title'] = $xianshi['xianshi_title'];
            $datas['xianshi_explain'] = $xianshi['xianshi_explain'];
            $datas['xianshi_discount'] = Discount($rea['goods_price'],I('xianshi_price'));
            $datas['store_id'] = $xianshi['store_id'];
            $datas['goods_name'] = $rea['goods_title'];
            $datas['goods_price'] = $rea['goods_price'];
            $datas['goods_image'] = $rea['goods_thumb'];
            $datas['start_time'] = $xianshi['start_time'];
            $datas['end_time'] = $xianshi['end_time'];
            $datas['lower_limit'] = $xianshi['lower_limit'];
            $datas['state'] = 1;
            $datas['xianshi_recommend'] = 0;
            $re = M('pXianshiGoods')->add($datas);
            if($re){
                $arr['result'] = TRUE;
                $arr['message'] = "商品添加成功！";
                $arr['xianshi_goods'] = M('pXianshiGoods')->where('xianshi_goods_id='.$re)->find();
            }else{
                $arr['result'] = FALSE;
                $arr['message'] = "商品添加失败！";
            }
        }
        return $this->ajaxReturn($arr);
    }
    
    //删除限时折扣的商品
    public function deleteGoods(){
        $Id = I('xianshi_goods_id');
        if (empty($Id)) {
            $arr['result'] = FALSE;
            $arr['message'] = '参数不能为空';
        }
        if (M('pXianshiGoods')->where('xianshi_goods_id=' . $Id)->delete()) {
            $arr['result'] = TRUE;
            $arr['message'] = '取消成功！';
        } else {
            $arr['result'] = FALSE;
            $arr['message'] = '删除失败!';
        }
        return $this->ajaxReturn($arr);
    }

    
    //删除满减送
    public function del(){
        
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('pMansong')->where('mansong_id=' . $Id)->delete()) {
            M('pMansongRule')->where('mansong_id=' . $Id)->delete();
            $this->success('删除成功！', U('index'));
        } else {
            $this->error('删除失败！');
        }
    }
    
    
}
